<?php
/**
 * Created by PhpStorm.
 * User: CYJiao
 * Date: 6/8/15
 * Time: 23:57
 */

class DoctorController extends Controller
{

    /**
     * Method returning filters array.
     */
    public function filters()
    {
        return array(
            'accessControl',//Performs access control for CRUD operations
        );
    }

    /**
     * Defines User Access Level.
     */
    public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array(),//Actions allowed
                'roles'=>array('@'),//Roles allowed to perform actions
            ),
            array('deny',
                'actions'=>array(),//Actions not allowed
                'roles'=>array('*'),//Roles not allowed to perform actions
            ),
        );
    }

    /**
     * Declares class-based actions.
     */
    public function actions(){
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'height'=>37,
                'minLength'=>4,
                'maxLength'=>6,
                'backColor'=>0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page'=>array(
                'class'=>'CViewAction',
            ),
        );
    }

    /******************************************************************************************
     * Following functions are getting the patient list and information of the doctor
     ******************************************************************************************/

    /**
     * Get Patient List for Navigation
     * @param bigint $pk_doctor_id
     */
    public function actionGetPatientList($pk_doctor_id){

        /* 1. confirm the doctor or delegate ID
         * 2. find the doctor or delegate information
         * 3. find the doctor or delegate 's relationship with patients
         * 4. find these patients information
         * 5. generate in JSON format and return
         * Example
         * {"total":4,"rows":[{"true_name":"\u77eb\u627f\u6d0b","gender":"1","dob":"1987-02-14"},{"true_name":"\u77eb\u6587\u9f99","gender":"1","dob":null},{"true_name":"chen","gender":"2","dob":"1962-07-25"},{"true_name":"\u674e\u5fd7\u5a1f","gender":"2","dob":"1988-12-05"}]}
         */

        $sql = "SELECT op.pk_outpatient_id, op.true_name, op.username, op.gender, op.dob FROM tbl_outpatients op WHERE pk_outpatient_id in (SELECT do.fk_outpatient_id FROM tbl_doctors_has_tbl_outpatients do WHERE do.fk_doctor_id=".$pk_doctor_id.") ORDER BY create_time desc";
        $outPatients = Yii::app()->db->createCommand($sql)->queryAll();

        foreach($outPatients as &$op){
            if($op["gender"]==1){
                $op["gender"]='男';
            }else if($op["gender"]==2){
                $op["gender"]='女';
            }else{
                $op["gender"]='保密';
            }
        }

        $count = TblDoctorsHasTblOutpatients::model()->countByAttributes(array('fk_doctor_id'=>$pk_doctor_id));
        $total = '{"total":'.$count.'}';
        $jsonencode = json_encode($outPatients);
        $row='{"rows":'.$jsonencode.'}';

        $result = json_encode(
            array_merge(
                json_decode($total, true),
                json_decode($row, true)
            )
        );

        echo $result;
    }

    /**
     * Get Patient details in Navigation
     * @param bigint $pk_outpatient_id
     */
    public function actionGetOutPatientDetail($pk_outpatient_id){

        $outpatientInfo = TblOutpatients::model()->findByAttributes(array('pk_outpatient_id'=>$pk_outpatient_id));

        if($outpatientInfo["gender"]==1){
            $gender="男";
        }else if($outpatientInfo["gender"]==2){
            $gender="女";
        }else{
            $gender="保密";
        }

        $html='<div class="easyui-panel" data-options="border:false,fit:true">';
            $html.='<table class="dv" style="width:100%;height:100px;" border="0">';
                $html.='<tbody>';
                    $html.='<tr>';
                        $html.='<td rowspan="3" style="width:60px">';
                        $html.='<img src="http://picserver.o2omedical.com:8080/o2o_pic_server'.$outpatientInfo["outpatient_file_url"].'scaling/'.$outpatientInfo["outpatient_file_id"].'_180_180'.$outpatientInfo["outpatient_file_suffix"].'?token=b226d434-052a-4a14-9c59-917173a12864" style="width:60px;margin-right:20px"></td>';
                        $html.='<td>姓名：</td>';
                        $html.='<td>'.$outpatientInfo["true_name"].'</td>';
                    $html.='</tr>';
                    $html.='<tr>';
                        $html.='<td>年龄：</td>';
                        $html.='<td>'.age($outpatientInfo["dob"]).'</td>';
                    $html.='</tr>';
                    $html.='<tr>';
                        $html.='<td>性别: </td>';
                        $html.='<td colspan="3">'.$gender.'</td>';
                    $html.='</tr>';
                    $html.='<tr>';
                        $html.='<td></td>';
                        $html.='<td>职业: </td>';
                        $html.='<td>'.$outpatientInfo["occupation"].'</td>';
                    $html.='</tr>';
                $html.='</tbody>';
            $html.='</table>';
        $html.='</div>';

        echo $html;
    }

}